package cn.kgc.viny.smbms.web;

import cn.kgc.viny.smbms.service.Constant;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

public class CheckUserLoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)
            throws Exception {
        String path = httpServletRequest.getServletPath();
        System.out.println("path = " + path+",contextPath="+httpServletRequest.getContextPath());
        Object user=httpServletRequest.getSession().getAttribute(Constant.USER);
        if(user==null){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+ File.separator+"toLogin");
            return false;
        }
        return true;
    }

}
